package study.cv.image.controller;

import cn.hutool.core.date.StopWatch;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.URLUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import study.cv.image.mapper.basresource.BasResourceEO;
import study.cv.image.mapper.basresource.BasResourceMapper;
import study.cv.image.mapper.examanscell.ExamAnsCellEO;
import study.cv.image.mapper.examanscell.ExamAnsCellMapper;
import study.cv.image.mapper.examanspage.ExamAnsPageEO;
import study.cv.image.mapper.examanspage.ExamAnsPageMapper;
import study.cv.image.support.utils.CSpaceUtils;
import study.eggs.result.Result;
import study.eggs.result.Results;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.InputStream;
import java.net.URI;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 图片坐标
 */
@Slf4j
@RestController
public class CoordController {

    private String t = "https://test.youneng.com/FileCenter/";

    @Autowired
    private ExamAnsPageMapper examAnsPageMapper;
    @Autowired
    private ExamAnsCellMapper examAnsCellMapper;
    @Autowired
    private BasResourceMapper basResourceMapper;

    /**
     * ==============================
     * 基于坐标裁剪
     * ==============================
     */
    @PostMapping("/clip")
    public Result clip(@RequestBody Request request) {
        //
        Long sheetId = request.getSheetId();
        List<ExamAnsPageEO> pageEOLt = examAnsPageMapper.getAnsPageLtBySheetId(sheetId);
        ExamAnsPageEO pageEO = pageEOLt.get(0);
        // 下载
        String resId = pageEO.getResId();
        BasResourceEO resEO = basResourceMapper.selectById(resId);
        String path = resEO.getPathReal();
        //
        URI uri = URLUtil.toURI(t + path);
        try (InputStream in = URLUtil.getStream(uri.toURL())) {
            //FileUtil.writeFromStream(in, new File(StrUtil.format("{}.png", sheetId)));
            BufferedImage img = ImageIO.read(in);
            //
            Long pageId = pageEO.getPageId();
            List<ExamAnsCellEO> cellEOLt = examAnsCellMapper.getAnsCellLtByPageId(pageId);
            int pageNo = 1;
            for (ExamAnsCellEO cellEO : cellEOLt) {
                // x y
                int x = cellEO.getTopX(), y = cellEO.getTopY();
                // with height
                int w = cellEO.getBottomX() - x, h = cellEO.getBottomY() - y;
                BufferedImage subImg = img.getSubimage(x, y, w, h);
                ImageIO.write(subImg, "png", new File(StrUtil.format("clip/{}.png", pageNo++)));
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return Results.ok();
    }

    /**
     * ==============================
     * 基于坐标绘制
     * ==============================
     */
    @PostMapping("/draw")
    public Result draw(@RequestBody Request request) {
        StopWatch stopWatch = StopWatch.create("耗时统计");

        //
        stopWatch.start("查询答题卡");
        Long sheetId = request.getSheetId();
        List<ExamAnsPageEO> pageEOLt = examAnsPageMapper.getAnsPageLtBySheetId(sheetId);
        ExamAnsPageEO pageEO = pageEOLt.get(0);
        stopWatch.stop();

        // 下载
        stopWatch.start("查询图片");
        String resId = pageEO.getResId();
        BasResourceEO resEO = basResourceMapper.selectById(resId);
        String path = resEO.getPathReal();
        stopWatch.stop();
        //
        URI uri = URLUtil.toURI(t + path);
        stopWatch.start("下载");
        try (InputStream in = URLUtil.getStream(uri.toURL())) {
            stopWatch.stop();
            //
            BufferedImage src = ImageIO.read(in);
            //
            stopWatch.start("换色");
            BufferedImage img = CSpaceUtils.toRGB(src);
            stopWatch.stop();

            stopWatch.start("创建Graphics");
            Graphics2D graphics2D = img.createGraphics();
            stopWatch.stop();
            //
            Font font = new Font("Serif", Font.ITALIC, 50);
            graphics2D.setFont(font);
            // 颜色
            graphics2D.setColor(Color.RED);

//            graphics2D.setStroke(new BasicStroke(5));
            //
            stopWatch.start("查询单元");
            Long pageId = pageEO.getPageId();
            List<ExamAnsCellEO> cellEOLt = examAnsCellMapper.getAnsCellLtByPageId(pageId);
            stopWatch.stop();
            stopWatch.start("绘制");
            for (ExamAnsCellEO cellEO : cellEOLt) {
                int x = cellEO.getBottomX() - 150, y = cellEO.getBottomY() - 80;
                //log.info("x= {}, y= {}", x, y);

                graphics2D.drawString("+10", x, y);
//                graphics2D.drawLine(cellEO.getTopX(), cellEO.getTopY(), cellEO.getBottomX(), cellEO.getBottomY());
            }
            stopWatch.stop();

            stopWatch.start("保存图片");
            ImageIO.write(img, "png", FileUtil.newFile("888888888888.png"));
            stopWatch.stop();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        log.info("{}", stopWatch.prettyPrint(TimeUnit.MILLISECONDS));
        return Results.ok();
    }

    @Data
    public static class Request {
        private Long sheetId;
    }
}
